import { defineStore } from 'pinia'
import type { ConsultType } from '@/enums'
import type { PartialConsult, ConsultIllness } from '@/types/consult'
import { ref } from 'vue'

export const useConsultStore = defineStore(
  'cp-consult', // 仓库的唯一标识
  () => {
    // 回调函数，返回想提供的状态
    // 定义仓库，提供:
    // 问诊记录状态
    const consult = ref<PartialConsult>({})
    //1. 修改问诊类型函数 设置问诊类型
    const setType = (type: ConsultType) => (consult.value.type = type)
    //2. 修改急速问诊类型函数
    const setIllnessType = (type: 0 | 1) => (consult.value.illnessType = type)
    //3. 修改科室函数
    const setDep = (id: string) => (consult.value.depId = id)
    //4. 修改病情描述相关信息函数
    const setIllness = (
      // 从一个对象里取出4项
      illness: ConsultIllness
    ) => {
      // 传进来的4个数据分别设置给consult
      consult.value.illnessDesc = illness.illnessDesc
      consult.value.illnessTime = illness.illnessTime
      consult.value.consultFlag = illness.consultFlag
      consult.value.pictures = illness.pictures
    }
    //5. 修改患者函数 设置患者
    const setPatient = (id: string) => (consult.value.patientId = id)
    //6. 修改优惠卷函数
    const setCoupon = (id?: string) => (consult.value.couponId = id)
    //7. 清空记录信息 清除前端本地的记录信息
    const clear = () => (consult.value = {})

    return { consult, setType, setIllnessType, setDep, setIllness, setPatient, setCoupon, clear }
  },
  {
    // 开启持久化（使用本地存储，默认是localStorage）
    persist: true
  }
)
